package net.zjitc.securityloginapi.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ICaptcha;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.zjitc.securityloginapi.captcha.MyCodeGenerator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;

/**
 * @author 罗翼侨
 * @date 2025/3/5 14:11
 */
@Controller
public class CaptchaController {
    @RequestMapping(value = "/common/captcha")
    public void generateCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("image/jpeg");
//        ICaptcha captcha = CaptchaUtil.createCircleCaptcha(100, 50, 5, 10,1);
//        ICaptcha captcha = CaptchaUtil.createCircleCaptcha(100, 50, new MyCodeGenerator(), 15);
        ICaptcha captcha = CaptchaUtil.createGifCaptcha(100, 50, new MyCodeGenerator(), 15);
//        ICaptcha captcha = CaptchaUtil.createGifCaptcha(100, 50, 4,15, 1);
        request.getSession().setAttribute("captcha", captcha.getCode());
        captcha.write(response.getOutputStream());
    }
}
